package com.game.thrones.controller.webpage;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.game.thrones.domain.webpage.WebBannerTypeDO;
import com.game.thrones.service.web.WebBannerTypeService;
import com.game.thrones.service.web.WebEndemicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.game.thrones.constant.Errors;
import com.game.thrones.controller.BaseController;
import com.game.thrones.controller.input.DataInput;
import com.game.thrones.controller.input.PageInput;
import com.game.thrones.controller.output.BaseOutput;
import com.game.thrones.controller.output.PageOutput;
import com.game.thrones.controller.output.ResultResponseBeanUtil;
import com.game.thrones.domain.KbmContent;
import com.game.thrones.domain.webpage.WebBannerDO;
import com.game.thrones.exception.GameGlobalException;
import com.game.thrones.service.web.WebBannerService;
import com.game.thrones.util.OSSUploadUtil;
import com.game.thrones.util.StringUtils;

//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;

//@Api(value = "网站-轮播图", description = "网站-轮播图-田春阳", produces = "application/json")
@Controller
public class WebBannerController extends BaseController {
	private Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	WebBannerService webBannerService;
	@Autowired
	WebEndemicService webEndemicService;
	@Autowired
	WebBannerTypeService webBannerTypeService;
//	@ApiOperation(value = "根据类型获取banner列表", notes = "根据类型获取banner列表")
	@ResponseBody
	@RequestMapping(value = "/web/banner/findListByType", method = RequestMethod.POST)
	public BaseOutput<List<WebBannerDO>> findListByType(HttpServletRequest request,
			@Valid @RequestBody DataInput input) {
		KbmContent kbmContent = webEndemicService.findKbmContent(request);
		List<WebBannerDO> entityList = webBannerService.findListByType(kbmContent, input.getDataId());
		for (WebBannerDO entity : entityList) {
			String url = entity.getImg();
			if (StringUtils.isNotEmpty(url)) {
				entity.setImgUrl(OSSUploadUtil.getUrl(url));
			}
		}
		return ResultResponseBeanUtil.success(entityList);
	}

//	@ApiOperation(value = "分页列表", notes = "分页列表")
	@ResponseBody
	@RequestMapping(value = "/web/banner/listByPage", method = RequestMethod.POST)
	public PageOutput<WebBannerDO> listByPage(HttpServletRequest request, @RequestBody PageInput input) {
		KbmContent kbmContent = getCurrentInfo(request);
		input.setKbmContent(kbmContent);
		List<WebBannerDO> entityList = webBannerService.findByPage(input);
		Integer total = webBannerService.countByPage(input);
		return ResultResponseBeanUtil.pageSuccess(entityList, input.getSize(), input.getPage(), total);
	}

//	@ApiOperation(value = "获取banner列表", notes = "获取banner列表")
	@ResponseBody
	@RequestMapping(value = "/web/banner/findList", method = RequestMethod.POST)
	public BaseOutput<List<WebBannerDO>> findList(HttpServletRequest request) {
		KbmContent kbmContent = webEndemicService.findKbmContent(request);
		List<WebBannerDO> entityList = webBannerService.findAll(kbmContent);
		for (WebBannerDO entity : entityList) {
			String url = entity.getImg();
			if (StringUtils.isNotEmpty(url)) {
				entity.setImgUrl(OSSUploadUtil.getUrl(url));
			}
		}
		return ResultResponseBeanUtil.success(entityList);
	}

//	@ApiOperation(value = "根据类型获取banner", notes = "根据类型获取banner")
	@ResponseBody
	@RequestMapping(value = "/web/banner/findOneByType", method = RequestMethod.POST)
	public BaseOutput<WebBannerDO> findOneByType(HttpServletRequest request, @Valid @RequestBody DataInput input) {
		KbmContent kbmContent = webEndemicService.findKbmContent(request);
		WebBannerDO entity = webBannerService.findOneByType(kbmContent, input.getDataId());
		if (entity != null) {
			String url = entity.getImg();
			if (StringUtils.isNotEmpty(url)) {
				entity.setImgUrl(OSSUploadUtil.getUrl(url));
			}
		}
		return ResultResponseBeanUtil.success(entity);
	}
//	@ApiOperation(value = "根据id获取banner", notes = "根据id获取banner")
	@ResponseBody
	@RequestMapping(value = "/web/banner/findOneById", method = RequestMethod.POST)
	public BaseOutput<WebBannerDO> findOneById(HttpServletRequest request, @Valid @RequestBody DataInput input) {
		KbmContent kbmContent = getCurrentInfo(request);
		if (kbmContent == null) {
			throw new GameGlobalException(Errors.MODIFY_ERROR);
		}
		WebBannerDO entity = webBannerService.findOneById(kbmContent, input.getDataId()) ;
		if (entity != null) {
			String url = entity.getImg();
			if (StringUtils.isNotEmpty(url)) {
				entity.setImgUrl(OSSUploadUtil.getUrl(url));
			}
		}
		return ResultResponseBeanUtil.success(entity);
	}
//	@ApiOperation(value = "新增", notes = "新增")
	@ResponseBody
	@RequestMapping(value = "/web/banner/add", method = RequestMethod.POST)
	public BaseOutput<Void> add(HttpServletRequest request, @Valid @RequestBody WebBannerDO input) {
		KbmContent kbmContent = getCurrentInfo(request);
		if (kbmContent == null) {
			throw new GameGlobalException(Errors.MODIFY_ERROR);
		}
		input.setKbmContent(kbmContent);
		webBannerService.add(input);
		return ResultResponseBeanUtil.success();
	}

//	@ApiOperation(value = "修改", notes = "修改")
	@ResponseBody
	@RequestMapping(value = "/web/banner/update", method = RequestMethod.POST)
	public BaseOutput<Void> update(HttpServletRequest request, @Valid @RequestBody WebBannerDO input) {
		KbmContent kbmContent = getCurrentInfo(request);
		if (kbmContent == null) {
			throw new GameGlobalException(Errors.MODIFY_ERROR);
		}
		input.setKbmContent(kbmContent);
		webBannerService.update(input);
		return ResultResponseBeanUtil.success();
	}

//	@ApiOperation(value = "删除", notes = "删除")
	@ResponseBody
	@RequestMapping(value = "/web/banner/delete", method = RequestMethod.POST)
	public BaseOutput<Void> delete(HttpServletRequest request, @Valid @RequestBody DataInput input) {
		KbmContent kbmContent = getCurrentInfo(request);
		WebBannerDO entity = webBannerService.findOneById(kbmContent, input.getDataId());
		if (entity == null) {
			throw new GameGlobalException(Errors.DATA_NOTEXIST);
		}
		entity.setKbmContent(kbmContent);
		webBannerService.del(entity);
		return ResultResponseBeanUtil.success();
	}

//	@ApiOperation(value = "类型列表", notes = "类型列表")
	@ResponseBody
	@RequestMapping(value = "/web/banner/typeList", method = RequestMethod.POST)
	public BaseOutput<List<WebBannerTypeDO>> typeList(HttpServletRequest request ) {
		KbmContent kbmContent = getCurrentInfo(request);
		return ResultResponseBeanUtil.success(webBannerTypeService.findAll( ));
	}
}
